﻿@inherits AdminCompontentBase

<MNavigationDrawer Stateless Touchless Temporary Fixed Right Value=Visible ValueChanged=VisibleChanged Width="465" Class="pa-6">
    <div class="block-between mb-12">
        <span class="text-h6">@T("Add Authoize")</span>
        <MIcon Color="neutral-lighten-3" Size=24 OnClick="async ()=> await UpdateVisible(false)">mdi-close</MIcon>
    </div>

    <MForm Model=AuthoriedData EnableValidation>       
        <MSelect
                 Value="@AuthoriedData.ObjectType"
                 ValueChanged="value => ObjectValueChanged(value)"
                 Color="primary"
                 HideDetails="@("auto")"
                 Outlined
                 TValue="ObjectType"
                 TItemValue="ObjectType"
                 TItem="KeyValuePair<string, ObjectType>"
                 Items="@(ComponentPageBase.GetEnumMap<ObjectType>())"
                 ItemText="map => map.Key"
                 ItemValue="map => map.Value"
                 Label="@T("ObjectType")">
        </MSelect>

        <MTextField @bind-Value="AuthoriedData.Resource" Disabled="@(AuthoriedData.ObjectType is ObjectType.Menu)" Class="mt-6" Label="@T("Resource")" HideDetails="@("auto")" Outlined />

        @if(AuthoriedData.ObjectType == ObjectType.Api)
        {
            <MTextarea @bind-Value="@AuthoriedData.Scope" Class="mt-6" Label="@T("Scop")" HideDetails="@("auto")" Outlined />
        }
        else if(AuthoriedData.ObjectType == ObjectType.Menu)
        {            
            <div style="min-height:200px;border:1px solid black;" class="pa-2 max-width mt-6 hover-pointer"  @onclick="() => OpenAddMenuDialog = true" >
                <span>@T("Scope")：</span>
                @foreach(var menuName in SelectMenuNames.Split(','))
                {
                    if(menuName != "")
                    {
                      <MChip Class="ma-2" Color="pink" TextColor="white" Label>
                        <MIcon Left> mdi-label </MIcon>
                        @menuName
                      </MChip> 
                    }
                }
            </div>          
        }

        <MTextField @bind-Value="AuthoriedData.Action" Class="mt-6" Label="@T("Action")" HideDetails="@("auto")" Outlined />

        <MSelect
                 @bind-Value="@AuthoriedData.PermissionType"
                 Class="mt-6"
                 Color="primary"
                 HideDetails="@("auto")"
                 Outlined
                 Items="@(ComponentPageBase.GetEnumMap<PermissionType>())"
                 ItemText="map => map.Key"
                 ItemValue="map => map.Value"
                 Label="@T("PermissionType")">
        </MSelect>

        <MTextField @bind-Value="AuthoriedData.PermissionName" Class="mt-6" Label="@T("PermissionName")" HideDetails="@("auto")" Outlined />
       
        <div style="bottom:48px;right:24px;position:absolute">
            <MButton MinWidth=80 Height=40 Outlined Class="text-btn rounded-pill" OnClick="async() => await UpdateVisible(false)">@T("Cancel")</MButton>
            <MButton MinWidth=80 Height=40 Color="primary" Class="ml-6 rounded-pill" OnClick="async ()=> await AddAuthorizeeAsync(context)">@T("Confirm")</MButton>
        </div>
    </MForm>
</MNavigationDrawer>

<AddMenuDialog @bind-Visible=OpenAddMenuDialog SelectMenuCodes=@SelectMenuCodes SelectMenuCodesChanged=SelectMenuCodesChanged SelectMenuNamesChanged=SelectMenuNamesChanged />

@code {

    [Inject]
    public MenuPage MenuPage { get; set; } = default!;

    public bool OpenAddMenuDialog { get; set; }

    [Parameter]
    public bool? Visible { get; set; }

    [Parameter]
    public EventCallback<bool?> VisibleChanged { get; set; } 

    public AuthorizeItemResponse AuthoriedData { get; set; } = new();

    [Parameter]
    public EventCallback<AuthorizeItemResponse> ConfirmHandle { get; set; }

    public List<KeyValuePair<string, ObjectType>> ObjectTypeSelect => ComponentPageBase.GetEnumMap<ObjectType>();

    public string SelectMenuCodes { get; set; } = "";

    public string SelectMenuNames { get; set; } = "";

    void SelectMenuCodesChanged(string selectMenuCodes)
    {
        AuthoriedData.Scope = selectMenuCodes;
        SelectMenuCodes = selectMenuCodes;
    }

    void SelectMenuNamesChanged(string selectMenuNames)
    {
        SelectMenuNames = selectMenuNames;
    }

    public void ObjectValueChanged(ObjectType type)
    {
        AuthoriedData.ObjectType = type;
        AuthoriedData.Scope = "";
        SelectMenuCodes = "";
        SelectMenuNames = "";
        if(type == ObjectType.Menu)
        {
            AuthoriedData.Resource="menus";
        }
        else
        {
            AuthoriedData.Resource="";
        }
    }

    private async Task UpdateVisible(bool visible)
    {
        if (VisibleChanged.HasDelegate)
        {
            await VisibleChanged.InvokeAsync(visible);
        }
        else
        {
            Visible = visible;
        }
    }

    private async Task AddAuthorizeeAsync(FormContext context)
    {
        if (context.Validate())
        {
            await ConfirmHandle.InvokeAsync(AuthoriedData);
            await UpdateVisible(false);
        }
    }

    protected override void OnParametersSet()
    {
        SelectMenuCodes = "";
        SelectMenuNames = "";
        if (Visible is true)
        {
            AuthoriedData = new();       
        }
    }
}
